package io.zcoder.innoway.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import io.zcoder.innoway.R;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.models.EntryTime;
import io.zcoder.innoway.widgets.TextViewCustom;


public class EntryTimeAdapter extends RecyclerView.Adapter<EntryTimeAdapter.EntryPlaceHolder> {

    private List<EntryTime> mEntryTimes;
    private String mFormatTime;
    private String mFormatResponse;
    private String mUnit;

    public EntryTimeAdapter(List<EntryTime> entryTimes, String formatTime, String formatResponse, String unit) {
        mEntryTimes = entryTimes;
        mFormatTime = formatTime;
        mFormatResponse = formatResponse;
        mUnit = unit;
    }

    public class EntryPlaceHolder extends RecyclerView.ViewHolder {
        TextViewCustom mTime, mValue;

        public EntryPlaceHolder(View itemView) {
            super(itemView);
            mTime = (TextViewCustom) itemView.findViewById(R.id.time);
            mValue = (TextViewCustom) itemView.findViewById(R.id.value);
        }
    }


    @Override
    public EntryPlaceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_entry_time, parent, false);

        return new EntryPlaceHolder(itemView);
    }

    @Override
    public void onBindViewHolder(EntryPlaceHolder holder, int position) {
        EntryTime entryTime = mEntryTimes.get(position);
        holder.mValue.setText(entryTime.getValueWithUnit(mUnit));
        holder.mTime.setText(Utils.convertToString(Utils.convertToDate(entryTime.getTime(), mFormatResponse), mFormatTime));
    }


    @Override
    public int getItemCount() {
        return mEntryTimes == null ? 0 : mEntryTimes.size();
    }

}
